import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import an49 from '@mmstudio/an000049';
import { tbclass } from '../../../atoms/db';

const logger = anylogger('pages/api/class/bianji.api');

export type Result = {
	ok: true;
} | {
	ok: false;
	message: string;
};

export type Message = ITbclass;

/**
 * 编辑
 */
const handler = an48<Result>();

handler.post(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);
		const { class_id, ...body } = req.body as Message;

		if (!body
			|| !
			class_id




			|| !body.grade
			|| !body.
				majorname








			|| !body.classname
			|| !body.monitorname
			|| !body.studycommitteename
		) {
			res.status(200).json({
				ok: false,
				message: '缺少必要的参数'
			});
			return;
		}
		// 检查修改后的班级名称是否存在
		const classes = await tbclass().first('*')
			.where({
				classname: body.classname,
				monitorname: body.monitorname,
				studycommitteename: body.studycommitteename
			}).andWhereNot({

				class_id




			});
		if (classes) {
			res.status(200).json({ ok: false, message: '内容已存在' });
			return;
		}
		await tbclass().update(body).where({

			class_id




		});
		res.status(200).json({ ok: true });
	} catch (error) {
		logger.trace(error);
		res.status(200).json({ ok: false, message: (error as Error).message });
	}
});

export const config = {} as PageConfig;

export default handler;
